package com.one.service.impl;


import com.one.dto.*;
import com.one.entity.ElectiveInfoEntity;
import com.one.entity.EvaluateResultDetailEntity;
import com.one.entity.SemesterInfoEntity;
import com.one.entity.SysCollegeEntity;
import com.one.mapper.SemesterInfoMapper;
import com.one.service.SemesterInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 批次表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-08
 */
@Service
public class SemesterInfoServiceImpl extends ServiceImpl<SemesterInfoMapper, SemesterInfoEntity> implements SemesterInfoService {
    private final SemesterInfoMapper semesterInfoMapper;
    public SemesterInfoServiceImpl(SemesterInfoMapper semesterInfoMapper){
        this.semesterInfoMapper = semesterInfoMapper;
    }
    @Override
    public Result selectBatchByCodeAndName(SemesterInfoEntity semesterInfoEntity) {
        if (semesterInfoEntity.getCode().equals("") && semesterInfoEntity.getName().equals("")) {
            return Result.error().message("编号和名称不能都为空");
        }
        return Result.ok().data(semesterInfoMapper.selectBatchByCodeAndName(semesterInfoEntity));
    }
    @Override
    public Result selectBatchByCodeAndName(SelectBatch semesterInfoEntity) {
        if (semesterInfoEntity.getCode().equals("") && semesterInfoEntity.getName().equals("")) {
            return Result.error().message("编号和名称不能都为空");
        }
        return Result.ok().data(semesterInfoMapper.selectBatchByCodeAndName(semesterInfoEntity));
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result insertBatch(SelectBatch semesterInfoEntity) {
        if(selectBatchByCodeAndName(semesterInfoEntity).getData()!=null){
            return Result.error().message("该批次已经存在了");
        }
        if (semesterInfoEntity.getEndDate().before(semesterInfoEntity.getStartDate())){
            return Result.error().message("开始时间要小于结束时间哟");
        }
        return Result.ok().data(semesterInfoMapper.insertBatch(semesterInfoEntity));
    }

    @Override
    public Result selectBatch(SemesterInfoEntity semesterInfoEntity) {
        int page = semesterInfoEntity.getPage();
        semesterInfoEntity.setPage(PaginationUtils.getPageIndex(semesterInfoEntity.getPage(), semesterInfoEntity.getNumber()));
        List<Object> result=semesterInfoMapper.selectBatch(semesterInfoEntity);
        int sum=((List<Integer>) result.get(1)).get(0);
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(semesterInfoEntity.getNumber(), page, sum);
        if (PaginationUtils.isValid(paginationDate)) {
            return Result.error().message("分页参数错误!");
        }
        return Result.ok().data(new ReturnDate(paginationDate,(List<SysCollegeEntity>) result.get(0) ));
    }

    @Override
    public Result updateBatch(SelectBatch semesterInfoEntity) {
        /*if(selectBatchByCodeAndName(semesterInfoEntity).getData()!=null){
            return Result.error().message("该批次已经存在了");
        }*/
        if (semesterInfoEntity.getEndDate().before(semesterInfoEntity.getStartDate())){
            return Result.error().message("开始时间要小于结束时间哟");
        }
        return Result.ok().data(semesterInfoMapper.updateBatch(semesterInfoEntity));
    }

    @Override
    public Result deleteBatch(SemesterInfoEntity  semesterInfoEntity) {
        return Result.ok().data(semesterInfoMapper.deleteBatch(semesterInfoEntity));
    }

    @Override
    public Result updateBatchStatus(SemesterInfoEntity semesterInfoEntity) {
        return Result.ok().data(semesterInfoMapper.updateBatchStatus(semesterInfoEntity));
    }

    @Override
    public Result insert(InsertIntoEvaluateResultDetail semesterInfoEntity) {
        Integer[] ids = semesterInfoEntity.getIds();
        int semcode = semesterInfoEntity.getSemCode();
        for (Integer id : ids) {
            ElectiveInfoEntity infoEntity = new ElectiveInfoEntity();
            EvaluateResultDetailEntity detailEntity = new EvaluateResultDetailEntity();
            infoEntity = semesterInfoMapper.selectInfo(id);
            if (infoEntity!=null) {
                detailEntity.setSemCode(String.valueOf(semcode));
                detailEntity.setTeachCode(infoEntity.getCeachCode());
                detailEntity.setElectiveId(infoEntity.getId());
                EvaluateResultDetailEntity detail = new EvaluateResultDetailEntity();
                detail = semesterInfoMapper.selectInfoBy(detailEntity);
                if (detail==null){
                    int rel = semesterInfoMapper.insertinfo(detailEntity);
                    if (rel<1){
                        return Result.error().message("添加失败");
                    }
                }
            }
        }
        return Result.ok();
    }
}
